﻿using System;
using System.Collections.Generic;

namespace 解释器模式Demo
{
    public abstract class Expression
    {
        static Dictionary<char,int> ShuZiPinYinDict = new Dictionary<char, int>()
        {
            {'一',1 },
            {'二',2 },
            {'三',3 },
            {'四',4 },
            {'五',5 },
            {'六',6 },
            {'七',7 },
            {'八',8 },
            {'九',9 },
            {'十',10 },
        };
        public abstract string One { get; }
        public abstract string Four { get; }
        public abstract string Five { get; }
        public abstract string Nine { get; }
        public abstract int Multiplier { get;}
        public void Interpret(Context context)
        {
            if (context.Input.Length == 0)
                return;
            if (context.Input.StartsWith(Nine))
            {
                context.Output += (9 * Multiplier);
                context.Input = context.Input.Substring(2);
            }
            else if (context.Input.StartsWith(Four))
            {
                context.Output += (4 * Multiplier);
                context.Input = context.Input.Substring(2);
            }
            else if (context.Input.StartsWith(Five))
            {
                context.Output += (5 * Multiplier);
                context.Input = context.Input.Substring(1);
            }
            while (context.Input.StartsWith(One))
            {
                context.Output += (1 * Multiplier);
                context.Input = context.Input.Substring(1);
            }
        }
    }

    public class ThousandExpression : Expression
    {
        public override string Five { get; } = " ";
        public override string Four { get; } = " ";
        public override string Nine { get; } = " ";
        public override string One { get; } = "M";
        public override int Multiplier { get; } = 1000;
    }

    public class HundredExpression : Expression
    {
        public override string Five { get; } = "D";
        public override string Four { get; } = "CD";
        public override string Nine { get; } = "CM";
        public override string One { get; } = "C";
        public override int Multiplier { get; } = 100;
    }

    public class TenExpression : Expression
    {
        public override string Five { get; } = "L";
        public override string Four { get; } = "XL";
        public override string Nine { get; } = "LC";
        public override string One { get; } = "X";
        public override int Multiplier { get; } = 10;
    }
    public class OneExpression : Expression
    {
        public override string Five { get; } = "V";
        public override string Four { get; } = "IV";
        public override string Nine { get; } = "IX";
        public override string One { get; } = "I";
        public override int Multiplier { get; } = 1;
    }
}